<?php
/**
 *
 * controller 页面控制器
 *
 */
class page {
	protected $_config;

	function __construct($config) {
		$this->_config = $config;
	}

	/**
	 *
	 * 得到quick view
	 *
	 */
	function actionIndex() {
		require_once './include/quickview.php';

		// 得到导航内容
		$quick = new quickview($this->_config);
		$quick_contents = $quick->html();

		//print_r($quick_contents);
		ob_start();
		include 'view/quick_view.php';
		$content = ob_get_clean();

		// layout
		require 'layout/phpvimwiki.php';
	}

	/**
	 *
	 * 显示wiki
	 *
	 */
	function actionView() {
		// $_GET['wiki']没有指定, 到index首页
		if(!$this->_config['wiki']) {
			$this->actionIndex();
			return;
		}
		// 处理获得的wiki路径
		$this->_fixWikiPath();

		// 查看 OR 建wiki
		// 是否有该wiki, 没有则创建
		$wiki_path = $this->_config['wiki_root']. '/'. $this->_config['wiki']. '.wiki';
		if(!file_exists($wiki_path)) {
			echo $wiki_path;
			$this->actionCreate();
			return;
		}
		// 存在, 解析之, 显示
		$wiki_content = file_get_contents($wiki_path);
		$phpvimwiki = new phpvimwiki($wiki_content, $this->_config['wiki']);
		$html = $phpvimwiki->html();

		$content = '<div id="article">'. $html['content']. '</div>';
		$title = $html['title'];

		require 'layout/phpvimwiki.php';
	}

	/**
	 * 
	 * 建wiki
	 *
	 */
	public function actionCreate() {
		echo 'create wiki';
	}

	/**
	 *
	 * config['wiki'] = 'php/life'
	 *	= '../path/../../../life'; 不行
	 *
	 */
	protected function _fixWikiPath() {
		$this->_config['wiki'] = str_replace('..', '.', $this->_config['wiki']);
	}
}
?>
